package org.hackystat.projectbrowser.page.setaction;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.link.Link;
import org.hackystat.ambientdevice.server.jaxb.Action;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.TriggerActionPair;
import org.hackystat.projectbrowser.ProjectBrowserSession;

/**
 * The class link that deletes an action.
 * 
 * @author Edward Kim
 * 
 */
public class DeleteActionLink extends Link {

  /**
   * Supports Serialization.
   */
  private static final long serialVersionUID = 1L;
  private List<TriggerActionPair> pairs = new ArrayList<TriggerActionPair>();
  private String actionId;
  private Project project;
  private String tapName;

  /**
   * Constructor.
   * 
   * @param id A string passed on to the super (not really needed).
   * @param actionId Component ID, supplied by Wicket markup.
   * @param projectName The name of the project to be deleted.
   * @param tapName The name of the trigger/action pair.
   */
  public DeleteActionLink(String id, String actionId, String projectName, String tapName) {
    super(id);
    this.actionId = actionId;
    this.tapName = tapName;
    this.project = ProjectBrowserSession.get().getProjectTypeXMLByNameId(projectName);
    pairs = project.getTriggerActionPairs().getTriggerActionPair();
  }

  /**
   * Deletes an action on click.
   */
  @Override
  public void onClick() {

    deleteNegativeAction(this.actionId);
    deletePositiveAction(this.actionId);
  }

  /**
   * Deletes a negative action.
   * 
   * @param actionId The id of the action to delete.
   */
  public void deleteNegativeAction(String actionId) {

    for (TriggerActionPair tapTemp : pairs) {
      List<Action> listActions = new ArrayList<Action>();
      if (tapTemp.getId().equals(this.tapName) && tapTemp.getNegativeActions() != null) {
        for (Action tempAction : tapTemp.getNegativeActions().getAction()) {
          if (tempAction.getId().equals(actionId)) {
            // tapTemp.getNegativeActions().getAction().remove(tempAction);
            listActions.add(tempAction);
          }
        }
        tapTemp.getNegativeActions().getAction().removeAll(listActions);
      }
    }
  }

  /**
   * Deletes a postive action.
   * 
   * @param actionId The id of the action to delete.
   */
  public void deletePositiveAction(String actionId) {
    for (TriggerActionPair tapTemp : pairs) {
      List<Action> listActions = new ArrayList<Action>();
      if (tapTemp.getId().equals(this.tapName) && tapTemp.getPositiveActions() != null) {
        for (Action tempAction : tapTemp.getPositiveActions().getAction()) {
          if (tempAction.getId().equals(actionId)) {
            // tapTemp.getPositiveActions().getAction().remove(tempAction);
            listActions.add(tempAction);
          }
        }
        tapTemp.getPositiveActions().getAction().removeAll(listActions);
      }
    }
  }
}
